﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HRMSys.Model;
using HRMSys.DAL;
using System.IO;

namespace HRMSys.UI.SystemMgr
{
    /// <summary>
    /// EmployeeEditUI.xaml 的交互逻辑
    /// </summary>
    public partial class EmployeeEditUI : Window
    {
        public bool IsInsert { get; set; }  //是否是新增的
        public Guid EditingId { get; set; } //正在编辑的员工的Id

        public EmployeeEditUI()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitBaseInfo(); //初始化一些基础信息

            if (IsInsert == true) //如果是新增, 先初始化一些默认值
            {
                Employee employee = new Employee();
                employee.InDate = DateTime.Today;   //入职日期
                employee.ContractStartDay = DateTime.Today; //合同签订日期
                employee.ContractEndDay = DateTime.Today.AddYears(1);   //合同到期日期
                employee.Nationality = "汉族";

                gridEmployee.DataContext = employee;
            }
            else    //如果是修改.
            {
                EmployeeDAL dal = new EmployeeDAL ();
                Employee employee = dal.GetById(EditingId);
                gridEmployee.DataContext = employee;
            }
        }

        /// <summary>
        /// 初始化一些基础信息
        /// </summary>
        private void InitBaseInfo()
        {
            /*初始化基础信息*/
            IdNameDAL idNameDAL = new IdNameDAL();
            cbGender.ItemsSource = idNameDAL.GetByCategory("性别");
            cbMarriage.ItemsSource = idNameDAL.GetByCategory("婚姻状况");
            cbPartyStatus.ItemsSource = idNameDAL.GetByCategory("政治面貌");
            cbEducation.ItemsSource = idNameDAL.GetByCategory("学历");

            /*初始化部门信息*/
            DepartmentDAL departDAL = new DepartmentDAL();
            Department[] departs = departDAL.GetAll(true);
            cbDepatment.ItemsSource = departs;

            /*初始化岗位信息*/
            PositionDAL positionDAL = new PositionDAL();
            Position[] positions = positionDAL.GetAll();
            cbPosition.ItemsSource = positions;
        }

        /// <summary>
        /// 保存按纽点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (IsInsert == true)
            {
                Employee employee = (Employee)gridEmployee.DataContext;
                int result = (new EmployeeDAL()).Insert(employee);
                if (result==1)
                {
                    MessageBox.Show("添加成功!","系统提示");

                    //添加员工成功后保存操作日志
                    (new OperationDAL()).Insert(CommonHelper.GetOperatoId(), "新增员工" + employee.Name);
                }
            }
            else 
            {
                Employee employee = (Employee)gridEmployee.DataContext;
                EmployeeDAL dal = new EmployeeDAL();
                dal.Update(employee);
                //保存操作日志
                (new OperationDAL()).Insert(CommonHelper.GetOperatoId(), "修改员工" + employee.Name);
            }
            DialogResult = true;
        }

        private void btnCapture_Click(object sender, RoutedEventArgs e)
        {
            PreviewUI ui = new PreviewUI();
            ui.ShowDialog();
            //if (ui.ShowDialog() == true)
            //{
            //    byte[] imageData = ui.CaptureData;
            //    string picPath = @"g:\123.jpg";
            //    FileStream fs = new FileStream(picPath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
            //    BinaryWriter bw = new BinaryWriter(fs);
            //    bw.Write(imageData, 0, imageData.Length);
            //    bw.Close();
            //    fs.Close();
            //}
        }
    }
}
